package com.zhu.transaction.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Zhu
 * @createtime 2021/1/7-18:43
 */
public class MyUtils {
    private MyUtils() {
    }

    public static String getRandomID() {
        String sdf = new SimpleDateFormat("yyyyMMddHHmmSS").format(new Date());
        return sdf + String.valueOf((Math.random() * 9 + 1) * 1000000).substring(0, 4);
    }

    /**
     * 浏览器字符解码
     *
     * @param str 需解码的字符串
     * @return 返回utf-8字符编码字符串
     */
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }

    /*    public static String createOrderId(String userID, String productId) {
            String sdf = new SimpleDateFormat("HHmmss").format(new Date());
            return userID.substring(0, 4) + productId.substring(0, 4) + sdf + String.valueOf((Math.random() * 9 + 1) * 1000000).substring(0, 2);
        }*/
    public static String createOrderId(String userID, String productId) {
        String sdf = new SimpleDateFormat("HHmm").format(new Date());
        return userID.substring(13) + productId.substring(13) + sdf;
    }

    //把JavaBean转化为map
    public static Map<String,String> bean2map(Object bean) throws Exception{
        Map<String,String> map = new HashMap<>();
        //获取JavaBean的描述器
        BeanInfo b = Introspector.getBeanInfo(bean.getClass(),Object.class);
        //获取属性描述器
        PropertyDescriptor[] pds = b.getPropertyDescriptors();
        //对属性迭代
        for (PropertyDescriptor pd : pds) {
            //属性名称
            String propertyName = pd.getName();
            //属性值,用getter方法获取
            Method m = pd.getReadMethod();
            String properValue = m.invoke(bean).toString();//用对象执行getter方法获得属性值

            //把属性名-属性值 存到Map中
            map.put(propertyName, properValue);
        }
        return map;
    }
}
